﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Sampling
{
    public partial class TaskAllocation : PageBase
    {
        UsersBO ubo = new UsersBO();
        LogBO lbo = new LogBO();
        PPARBO pbo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = pbo.GetTaskAllocation();
                if (ds.Tables[0].Rows.Count == 0)
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr[1] = "No data";
                    ds.Tables[0].Rows.Add(dr);
                    btnSubmit.Visible = false;
                }
                GridView1.DataSource = ds;
                GridView1.DataBind();
                ////if (Request.QueryString["RID"] != null)
                ////{
                ////    DataSet ds = sbo.GetList(" pparid=" + Request.QueryString["RID"].ToString());
                ////    foreach (DataRow dr in ds.Tables[0].Rows)
                ////    {
                ////        foreach (GridViewRow item in GridView1.Rows)
                ////        {
                ////            DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                ////            TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                ////            CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                ////            //ddlCon.Enabled = txtEnd.Enabled = cbSe.Checked;
                ////            if (dr["TESTDEPARTMENTID"].ToString() == ddlCon.CssClass)
                ////            {
                ////                cbSe.Checked = true;
                ////                cbSe.Enabled = false;
                ////                cbSe.BorderColor = System.Drawing.Color.CadetBlue;
                ////                ddlCon.SelectedValue = dr["DEPTREP"].ToString();
                ////                txtEnd.Text = DateTime.Parse(dr["ENDTIMEPLAN"].ToString()).ToString("yyyy-MM-dd");
                ////                continue;
                ////            }
                ////        }
                ////    }
                ////    if (Request.QueryString["id"] != null)
                ////    {
                ////        foreach (GridViewRow item in GridView1.Rows)
                ////        {
                ////            DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                ////            TextBox txtEnd = item.FindControl("TextBox1") as TextBox;
                ////            CheckBox cbSe = item.FindControl("cbSe") as CheckBox;
                ////            if (Request.QueryString["id"].ToString() == ddlCon.CssClass)
                ////            {
                ////                cbSe.Checked = true;
                ////                cbSe.Enabled = true;
                ////                ddlCon.Enabled = true;
                ////                txtEnd.Enabled = true;
                ////            }
                ////            else
                ////                cbSe.Enabled = false;
                ////        }
                ////    }
                ////}
            }
        }

        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int num = 0;
                foreach (GridViewRow item in GridView1.Rows)
                {

                    DropDownList ddlCon = item.FindControl("DropDownList1") as DropDownList;
                    if (ddlCon != null&&ddlCon.SelectedValue!="")
                    {
                        PPAR pp = pbo.GetModel(int.Parse(ddlCon.CssClass));
                        if (pp != null)
                        {
                            pp.Qeuser = ddlCon.SelectedValue;
                            num += pbo.UpdateForPPARTaskAllocation(pp);
                            lbo.WriteLog(CurrentUser.Id, "TaskAllocation", ddlCon.CssClass);
                        }
                    }
                }

                if (num > 0)
                {
                    AlertReLoad("Submit success");
                }


            }
            catch (Exception ex)
            {
                AlertMessage("Err:" + ex.Message.ToString());
            }
        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList dt = e.Row.FindControl("DropDownList1") as DropDownList;
                DataSet ds = ubo.GetList("  position='Q-engineer'");
                dt.Items.Add(new ListItem("undistributed", ""));
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    dt.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                }
            }
        }
    }
}